<?php

namespace app\admin\controller;

/**
 * 图片上传
 * Class Image
 * @package app\admin\controller
 */
class Image extends AdminBase {

    public function upload() {
        if (!$this->request->isPost()) {
            return show(config('status.error'), '非法请求');
        }
        $file = $this->request->file('file');
        // TODO 1 、上传图片类型需要判断 png gif jpg  2、文件大小限制
        //$filename = \think\facade\Filesystem::putFile('upload', $file);
        // 自定义上传目录
        $filename = \think\facade\Filesystem::disk('public')->putFile('image', $file);
        if (!$filename) {
            return show(config('status.error'), '上传失败');
        }
        $data = [
            'image' => '/upload/' . $filename
        ];
        return show(config('status.success'), '上传成功', $data);
    }

    /**
     * 富文本编辑器图片上传
     * @return \think\response\Json
     */
    public function layUpload() {
        if (!$this->request->isPost()) {
            return show(config('status.error'), '非法请求');
        }
        $file = $this->request->file('file');
        $filename = \think\facade\Filesystem::disk('public')->putFile('image', $file);
        if (!$filename) {
            return json(['code' => 1, 'data' => []], 200);
        }

        $result = [
            'code' => 0,
            'data' => [
                'src' => '/upload/' . $filename,
            ],
        ];
        return json($result, 200);
    }
}